import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:library_base/utils/get_util.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/car_entity.dart';
import 'package:library_base/entities/enum_entity.dart';
import 'package:library_base/entities/goods_child_entity.dart';
import 'package:library_base/entities/goods_child_info_entity.dart';
import 'package:library_base/entities/warehouse_entity.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/route/routers.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/utils/widget_generate_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/dialog/create_delivery_note_controller.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:library_base/ext/double_ext.dart';
import '../../utils/num_util.dart';

class GoodsListDialog extends StatefulWidget {
  const GoodsListDialog({
    Key? key,
  }) : super(key: key);

  @override
  State<GoodsListDialog> createState() => _GoodsListDialogState();
}

class _GoodsListDialogState extends State<GoodsListDialog> {
  /// 实体类
  List<GoodsChildEntity> listDataSelected = [];

  late void Function(List<GoodsChildEntity>) callback;

  @override
  void initState() {
    super.initState();
    var arguments = Get.arguments;
    callback = arguments['CallBack'];
    listDataSelected = arguments['listDataSelected'];
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        GetHelper.back();
        return Future.value(false);
      },
      child: GestureDetector(
        onTap: () {
          GetHelper.back();
        },
        child: Container(
          color: Colors.transparent,
          alignment: Alignment.bottomCenter,
          child: Container(
            decoration: tltrRs8,
            width: double.infinity,
            height: 0.6.sh,
            child: Column(
              children: [
                Stack(
                  children: [
                    Container(
                        alignment: Alignment.center,
                        padding: edgeAll24,
                        width: double.infinity,
                        child: Text(MultiLanguage.productSelected.tr, style: normalTextColor34)),
                    Positioned(
                      right: 0,
                      child: GestureDetector(
                        onTap: () => Get.back(),
                        child: Padding(
                            padding: edgeAll24,
                            child: LocalImage('common_close',
                                package: Constant.packLib(), width: 26.r, height: 26.r)),
                      ),
                    )
                  ],
                ),
                Expanded(
                  child: ListView(
                    children: buildItem(),
                  ),
                ),
                Container(
                    padding: EdgeInsets.only(top: 21.w, right: 24.w),
                    height: 166.w,
                    decoration: const BoxDecoration(color: Colors.white, boxShadow: [
                      BoxShadow(
                          offset: Offset(0.0, 5.0), //阴影xy轴偏移量
                          blurRadius: 15.0, //阴影模糊程度
                          spreadRadius: -11 //阴影扩散程度
                          )
                    ]),
                    alignment: Alignment.topRight,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(
                            child: Stack(children: [
                          Positioned(
                            left: 24.w,
                            top: 8.w,
                            child: LocalImage('admin_goods_cart',
                                package: Constant.packAdmin(), width: 56.w, height: 56.w),
                          ),
                          Positioned(
                            left: 100.w,
                            top: 20.w,
                            child: Text(
                                MultiLanguage.selectedGoodsCount.trArgs(["${listDataSelected.length}"])),
                          ),
                        ])),
                        GradientButton(
                          ThemeUtil.getTheme(),
                          width: 184.w,
                          height: 77.w,
                          text: MultiLanguage.determine.tr,
                          onPressed: listDataSelected.isEmpty
                              ? null
                              : () {
                                  GetHelper.back();
                                  callback(listDataSelected);
                                },
                        ),
                      ],
                    )),
              ],
            ),
          ),
        ),
      ),
    );
  }

  List<Widget> buildItem() {
    var childWidgets = listDataSelected.buildItem((index, element) {
      return Container(
          padding: EdgeInsets.symmetric(vertical: 19.w, horizontal: 24.w),
          child: Row(
            children: [
              Expanded(
                child: Container(
                  padding: edgeAll24,
                  child: Text(
                    element.goodsName,
                    style: normalTextColor30,
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {
                  listDataSelected.remove(element);
                  setState(() {});
                },
                child: Container(
                  color: Colors.transparent,
                  padding: edgeAll24,
                  child: Text(
                    MultiLanguage.remove.tr,
                    style: primaryTextColor30,
                  ),
                ),
              ),
            ],
          ));
    }, dividingLine: () {
      return line1;
    }, divideType: DivideType.MIDDLE_WITH_BOTTOM);
    return childWidgets;
  }
}
